<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>Bitset Constructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Bitsets</a> &gt; <a href=
    "bitset_constructors.html">Bitset Constructors</a>
  </div>

  <div class="name-format">
    Bitset Constructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;bitset&gt;
  bitset();
  bitset( unsigned long val );
</pre>

  <p>Bitsets can either be constructed with no arguments or with an
  unsigned long number val that will be converted into binary and
  inserted into the bitset. When creating bitsets, the number given in
  the place of the template determines how long the bitset is.</p>

  <p>For example, the following code creates two bitsets and displays
  them:</p>
  <pre class="example-code">
 // create a bitset that is 8 bits long
 bitset&lt;8&gt; bs;
 // display that bitset
 for( int i = (int) bs.size()-1; i &gt;= 0; i-- ) {
   cout &lt;&lt; bs[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;
 // create a bitset out of a number
 bitset&lt;8&gt; bs2( (long) 131 );
 // display that bitset, too
 for( int i = (int) bs2.size()-1; i &gt;= 0; i-- ) {
   cout &lt;&lt; bs2[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;            
</pre>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
